﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace JcomTesters.Releases
{
    //删除树
    [jDesc("删除树",ok:true)]
    class RmTreeCmd : Ab1Tester
    {
        public override void Run(string[] args)
        {
            var deb = new debugInfo();
            foreach (var item in args)
            {
                if (item.StartsWith("-debug")) { isDebug = true; continue; }
                //else if (item.StartsWith("-starts=")) { deb.startsWithList.Add( item.Substring("-starts=".Length)); continue; }
                //else if (item.StartsWith("-ends=")) { deb.endsWithList.Add(item.Substring("-ends=".Length)); continue; }
                else if (item.StartsWith("-")) { continue; }//包括参数，负数都会忽略
                else
                {
                    //if (string.IsNullOrEmpty(deb.dir)) { deb.dir = item; }
                    deb.dirs.Add(item);
                    continue;
                }
            }
            //if (string.IsNullOrEmpty(deb.dirs)
            //    || !System.IO.Directory.Exists(deb.dir)) { Console.WriteLine("目录为空或不存在"); return; }
            if (deb.dirs.Count == 0) { Console.WriteLine("请指定要删除的目录-可多个"); return; }
            //if (deb.isdebug)
            //{
            //    Console.WriteLine("dir: " + deb.dir);
            //    //Console.WriteLine("exists: "+deb.isdebug);
            //}
            //else
            //{
            //    deldirV2(deb.dir, deb.isdebug);
            //}
            foreach (var item in deb.dirs)
            {
                if (!System.IO.Directory.Exists(item)) { Console.WriteLine("删除前检查,目录不存在 " + item); continue; }
                deldirV2(item, isDebug);
            }
        }
        //换成深度
        void deldirV2(string dir1, bool isdebug = false)
        {
            var stList = new List<string>();
            //stList.Push(dir1);
            stList.Add(dir1);
            while (stList.Count != 0)
            {
                //var dir = stList.Peek();
                var dir = stList[stList.Count - 1];
                //bool all_file_deleted = true;
                //qList.Peek
                if (isdebug) Console.WriteLine(" peek dir: " + dir);
                //

                bool has_dirs = false;
                string[] get_dirs = null;
                try
                {
                    get_dirs = System.IO.Directory.GetDirectories(dir);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("出错了"); Console.WriteLine(ex.Message);
                    return;
                }
                foreach (var item in get_dirs)
                {
                    if (isdebug) Console.WriteLine("push x-dir: " + item);
                    //stList.Push(item);
                    ////这里居然需要判断是否存在，而且没有用
                    //if(Directory.Exists(item))
                    stList.Add(item);
                    has_dirs = true;
                    break;
                }
                //报错的目录
                //E:\hlProjects\pusher-admin-frontend-v4\node_modules\node-notifier\vendor\mac.noindex\terminal-notifier.app\Contents\Resources\en.lproj
                //E:\hlProjects\pusher-admin-frontend-v4\node_modules\node-notifier\vendor\mac.noindex\terminal-notifier.app\Contents
                //E:\hlProjects\pusher-admin-frontend-v4\node_modules\node-notifier\vendor\mac.noindex\terminal-notifier.app
                //E:\hlProjects\pusher-admin-frontend-v4\node_modules\normalize-url
                //E:\hlProjects\pusher-admin-frontend-v4\node_modules\progress-bar-webpack-plugin\node_modules\chalk
                //
                //del dir: E:\hlProjects\d3\react-admin\node_modules\ripemd160\
                //del dir: E:\hlProjects\d3\react-admin\node_modules\string-width\node_modules\ansi-regex\
                //
                //del dir: E:\hlProjects\d3\react-admin\node_modules\workbox-range-requests\build\

                if (has_dirs)
                {
                    continue;
                }
                var files = Directory.GetFiles(dir);
                foreach (var item in files)
                {
                    //if (System.IO.File.Exists(item))//mmm
                    //{
                    //if (isdebug) Console.WriteLine("del file: " + item);
                    Console.WriteLine("del file: " + item);
                    var art = File.GetAttributes(item);
                    if ((art & FileAttributes.Normal) != FileAttributes.Normal)
                    {
                        File.SetAttributes(item, FileAttributes.Normal);//不设为正常，有的只读的，会不能删除 System.UnauthorizedAccessException 访问拒绝
                    }
                    //System.IO.File.Delete(item);
                    //File.GetAttributes
                    try
                    {
                        //File.SetAttributes(item, FileAttributes.Normal);//不设为正常，有的只读的，会不能删除 System.UnauthorizedAccessException 访问拒绝
                        System.IO.File.Delete(item);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("出错了");
                        Console.WriteLine(ex.Message);
                        return;
                    }
                    //System.Threading.Thread.Sleep(5);//mmm
                    //}
                }

                //if (isdebug) Console.WriteLine("del dir: " + dir);
                //Console.WriteLine("del dir: " + dir+(dir.EndsWith("\\")?"": "\\"));
                Console.WriteLine("del dir: " + dir + "\\");
                //System.Threading.Thread.Sleep(80);
                //
                //
                //奇怪这里，有时会出错，在删 node_modules有时出现的提示，可能是draft-js\lib下的文件太多，删除时长不够
                //del file: E:\hlProjects\pusher-admin-frontend-v4\node_modules\draft-js\lib\_DraftTestHelper.js.flow
                //del dir: E:\hlProjects\pusher-admin-frontend-v4\node_modules\draft-js\lib\
                //
                //if(Directory.Exists(dir))//
                //未经处理的异常:  System.UnauthorizedAccessException: 对路径“E:\hlProjects\pusher-admin-frontend-v4\node_modules\node-notifier\vendor\mac.noindex\terminal-notifier.app\Contents”的访问被拒绝。
                //为什么判断了还是这错，
                try
                {
                    Directory.Delete(dir);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("出错了");
                    Console.WriteLine(ex.Message);
                    return;
                }
                if (isdebug) Console.WriteLine("pop dir: " + dir);
                //stList.Pop();
                stList.RemoveAt(stList.Count - 1);

                //if (all_file_deleted) { }
                //if (isdebug) Console.WriteLine("show dir: " + dir); //F:\fjiew0\d00\public
                //string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(dir);
                //if (isdebug) Console.WriteLine("ent len: " + fileSystemEntries.Length);
                //for (int i = 0; i < fileSystemEntries.Length; i++)
                //{
                //    string tar = fileSystemEntries[i];
                //    if (System.IO.File.Exists(tar))
                //    {
                //        if (isdebug) Console.WriteLine("del file: " + tar);
                //        File.SetAttributes(tar, FileAttributes.Normal);//不设为正常，有的只读的，会不能删除 System.UnauthorizedAccessException 访问拒绝
                //        System.IO.File.Delete(tar);
                //    }
                //    else
                //    {
                //        //if (isdebug) Console.WriteLine("into dir: " + tar);
                //        ////deldir(tar, isdebug);
                //        //if (System.IO.Directory.GetFileSystemEntries(dir).Length > 0)
                //        //{
                //        //if (isdebug) Console.WriteLine("Enqueue x-dir: " + tar);
                //        //    qList.Enqueue(tar);
                //        //    all_file_deleted = false;
                //        //}
                //        //else
                //        //{
                //        //    if (isdebug) Console.WriteLine("del x-dir: " + tar);
                //        //    Directory.Delete(tar);
                //        //}
                //        if (isdebug) Console.WriteLine("Enqueue x-dir: " + tar);
                //        qList.Enqueue(tar);
                //        all_file_deleted = false;
                //    }
                //}
                //if (all_file_deleted)
                //{
                //    if (isdebug) Console.WriteLine("del dir: " + dir);
                //    System.IO.Directory.Delete(dir);
                //}
                //else
                //{
                //    if (isdebug) Console.WriteLine("Enqueue dir: " + dir);
                //    qList.Enqueue(dir);
                //}
            }
        }
        //void deldir(string dir, bool isdebug = false)
        //{
        //    string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(dir);
        //    for (int i = 0; i < fileSystemEntries.Length; i++)
        //    {
        //        string tar = fileSystemEntries[i];
        //        if (System.IO.File.Exists(tar))
        //        {
        //            File.SetAttributes(tar, FileAttributes.Normal);//不设为正常，有的只读的，会不能删除 System.UnauthorizedAccessException 访问拒绝
        //            System.IO.File.Delete(tar);
        //            if (isdebug) Console.WriteLine("del file: " + tar);
        //        }
        //        else
        //        {
        //            if (isdebug) Console.WriteLine("into dir: " + tar);
        //            deldir(tar, isdebug);
        //        }
        //    }

        //    if (isdebug) Console.WriteLine("del dir: " + dir);
        //    System.IO.Directory.Delete(dir);
        //}
        class debugInfo
        {
            public List<string> dirs { get; set; } = new List<string>();
            //public List<string> startsWithList { get; set; } = new List<string>();
            //public List<string> endsWithList { get; set; } = new List<string>();
        }
    }
}
